package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserMessageDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;

import com.itheima.service.UserMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Service(interfaceClass = UserMessageService.class)
@Transactional
public class UserMessageServiceImpl implements UserMessageService {

    @Autowired
    private UserMessageDao userMessageDao;

    //分页查询
    public PageResult findUserMessage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);

        Page<User> page=userMessageDao.findUserMessage(queryString);

        List<User> result = page.getResult();

        return new PageResult(page.getTotal(),page.getResult());
    }

    //查询全部role角色
    public List<Role> findRoleAll() {

        List<Role> roles = userMessageDao.findRoleAll();
        for (Role role : roles) {
            System.out.println(role.getName());
        }
        return roles;
    }

    //新增用户
    public void addUserAndRoles(User user, Integer[] checkItem) {

        userMessageDao.addAndUser(user);
        Integer id = user.getId();

        if (checkItem!=null && checkItem.length>0){
            for (Integer roleId : checkItem) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("userId",id);
                map.put("roleId",roleId);
                userMessageDao.addAndRoles(map);
            }
        }
    }

    //编辑添加数据
    public User findUser(Integer id) {
       return userMessageDao.findUser(id);
    }


    public List<Integer> findAndRole(Integer userId) {

        List<Integer> rolesId = userMessageDao.findAndRole(userId);

        for (Integer integer : rolesId) {
            System.out.println(integer);
        }
        return rolesId;
    }

    //提交修改的数据
    public void updateUserAndRole(User user, Integer[] roleId) {

        Integer id = user.getId();

        userMessageDao.updateUser(user);
       //欲添加先清空
        userMessageDao.saveRoleAndUser(id);
        //添加
        if (roleId !=null && roleId.length>0){
            for (Integer roles : roleId) {
                Map<String, Integer> map = new HashMap<>();
                map.put("userId",id);
                map.put("roleId",roles);
                userMessageDao.addAndRoles(map);
            }
        }

    }

    //删除用户信息
    public void deleteUserAndRole(Integer userId) {

        Integer id=userId;

       userMessageDao.saveRoleAndUser(id);

       userMessageDao.deleteUser(id);


    }


    public long findUsername(String username) {

       long count= userMessageDao.findUsername(username);

        return count;
    }

}
